'use strict';

const Controller = require('egg').Controller;

class AnnounceController extends Controller {

  async announce() {

    // this.ctx.body = '通告页面';
    // 注意 ctx  异步
    // const mag = 'ejs';
    // const list = [ '111', '222', '333' ];
    // 调用服务里的方法

    const list = await this.service.announce.getgetAnnounceList;

    await this.ctx.render('announce', {
      // mag,
      list,
    });

  }

  async content() {
    // 获取get传值

    // koa中获取get传值 ctx.query

    // egg.js里面获取get传值
    const query = this.ctx.query;

    console.log(query);

    this.ctx.body = '通告详情';
  }
  /**
 * 获取公告列表接口
 */
  async getAnnounceList() {
    const { ctx } = this;
    //
    let retrunMap = {};
    try {
      const list = await this.ctx.service.announceService.getAnnounceList();

      if (list) {

        retrunMap = { code: '1', msg: '获取成功', data: list, total: list.length };
      } else {
        retrunMap = { code: '0', msg: '获取失败' };
      }
    } catch (err) {
      retrunMap = { code: '0', msg: '获取失败,发生异常' };
      console.log(err);
    }
    ctx.body = retrunMap;
  }

  /**
 * 查询已启用的科普文章
 */
  async getAnnounceListByQiYong() {
    const { ctx } = this;
    //
    let retrunMap = {};
    try {
      const list = await this.ctx.service.announceService.getAnnounceListByQiYong();

      if (list) {

        retrunMap = { code: '1', msg: '获取成功', data: list, total: list.length };
      } else {
        retrunMap = { code: '0', msg: '获取失败' };
      }
    } catch (err) {
      retrunMap = { code: '0', msg: '获取失败,发生异常' };
      console.log(err);
    }
    ctx.body = retrunMap;
  }

  /**
   * 通过城市编码获取公告列表接口
   */
  async getListByRegion() {
    const { ctx } = this;
    const cityCode = ctx.params.cityCode;
    let retrunMap = {};
    try {
      const list = await this.ctx.service.announceService.getListByRegion(cityCode);

      if (list) {

        retrunMap = { code: '1', msg: '获取成功', data: list, total: list.length };
      } else {
        retrunMap = { code: '0', msg: '获取失败' };
      }
    } catch (err) {
      retrunMap = { code: '0', msg: '获取失败,发生异常' };
      console.log(err);
    }
    ctx.body = retrunMap;
  }

  /**
 * 获取未审批公告列表接口
 */
  async getAnnounceListByApproval() {
    const { ctx } = this;
    //
    let retrunMap = {};
    try {
      const list = await this.ctx.service.announceService.getAnnounceListByApproval();

      if (list) {

        retrunMap = { code: '1', msg: '获取成功', data: list, total: list.length };
      } else {
        retrunMap = { code: '0', msg: '获取失败' };
      }
    } catch (err) {
      retrunMap = { code: '0', msg: '获取失败,发生异常' };
      console.log(err);
    }
    ctx.body = retrunMap;
  }

  /**
   * 保存公告接口
   */
  async saveAnnounce() {
    const { ctx } = this;
    ctx.body = ctx.request.body;
    // console.log(ctx.body);
    let retrunMap = {};
    try {
      const user_id = ctx.body.user_id;
      const user = await this.ctx.service.curdService.getDataByPk(user_id);
      ctx.body.region_id = user.region_id;
      const announce = await this.ctx.service.announceService.saveAnnounce(ctx.body);
      if (announce) {
        retrunMap = { code: '1', msg: '保存成功' };
      } else {
        retrunMap = { code: '0', msg: '保存失败' };
      }
    } catch (err) {
      retrunMap = { code: '0', msg: '保存失败,请联系管理员' };
      console.log(err);
    }
    ctx.body = retrunMap;
  }

  /**
   * 更新公告接口
   */
  async update() {
    const { ctx } = this;
    ctx.body = ctx.request.body;
    // console.log(ctx.body);
    let retrunMap = {};
    try {
      const announce = await this.ctx.service.announceService.update(ctx.body);
      if (announce) {
        retrunMap = { code: '1', msg: '更新成功' };
      } else {
        retrunMap = { code: '0', msg: '更新失败' };
      }
    } catch (err) {
      retrunMap = { code: '0', msg: '更新失败,发生异常' };
      console.log(err);
    }
    ctx.body = retrunMap;
  }

  /**
   * 审批公告接口
   */
  async approvingAnnounce() {
    const { ctx } = this;
    // 1.获取前端传来的请求授权信息
    ctx.body = ctx.request.body;
    console.log(ctx.body);
    let retrunMap = {};

    if (ctx.body.hasOwnProperty('id') && ctx.body.hasOwnProperty('result')) {
      const id = ctx.body.id; // 读取用户id
      const result = ctx.body.result; // 获取要求的授权状态
      let transaction;

      try {
        // 建立事务对象
        transaction = await this.ctx.model.transaction();
        let user;
        let status;
        if (result === '1') { // 审批通告
          status = 1;
          // 2.发起授权并更改数据库
          user = await this.ctx.service.announceService.approvingAnnounce(id, status, transaction);// 发,去service更改授权状态
        } else { // 审批拒绝
          status = 2;
          // 2.发起授权并更改数据库
          user = await this.ctx.service.announceService.approvingAnnounce(id, status, transaction);// 发去service更改授权状态
        }
        console.log(user);

        await this.ctx.service.approvalService.saveApplyOfAnnounce(ctx.body, transaction);// 保存审批记录

        retrunMap = { code: '1', msg: '审批提交成功' };// 封装成功授权返回报文
        // 提交事务
        await transaction.commit();
      } catch (err) {
        // 事务回滚
        await transaction.rollback();
        retrunMap = { code: '0', msg: '审批提交失败' };
        throw err;
      }

    } else { // 没有获取到，直接返回数据不正确
      retrunMap = { code: '0', msg: '审批提交失败' };
    }
    // 3.返回结果
    ctx.body = retrunMap;
  }

  /**
 * 模糊查询接口
 */
  async selectAnnouncesByLike() {
    const { ctx } = this;
    ctx.body = ctx.request.body;

    let retrunMap = {};
    try {
      const list = await this.ctx.service.announceService.selectAnnouncesByLike(ctx.body);
      if (list) {
        retrunMap = { code: '1', msg: '查询成功', data: list, total: list.length };
      } else {
        retrunMap = { code: '0', msg: '查询失败' };
      }
    } catch (err) {
      retrunMap = { code: '0', msg: '查询失败,发生异常' };
      console.log(err);
    }
    ctx.body = retrunMap;
  }

  /**
   * 删除公告接口
   */
  async deleteAnnounce() {
    const { ctx } = this;
    const id = ctx.params.id;
    const announce = await this.ctx.service.announceService.deleteAnnounce(id);

    ctx.body = announce;
  }
  /**
   * 公告详细界面数据接口
   */
  async getAnnounceDetail() {
    const { ctx } = this;
    ctx.body = ctx.request.body;
    let retrunMap = {};
    try {
      const announce = await this.ctx.service.announceService.getAnnounceDetail(ctx.body.announce_id);

      if (announce) {
        const subscribe = await this.ctx.service.subscribeService.getSubscribe(ctx.body.user_id, announce.region_id);
        //  const region = await this.ctx.service.regionService.getDataByPk(announce.region_id);
        retrunMap = { code: '1', msg: '查询成功', data: announce, subscribe };
      } else {
        retrunMap = { code: '0', msg: '查询失败' };
      }
    } catch (err) {
      retrunMap = { code: '0', msg: '查询失败,发生异常' };
      console.log(err);
    }
    ctx.body = retrunMap;
  }

  /**
   * 点赞
   */
  async addLiked() {
    const { ctx } = this;
    const id = ctx.params.id;
    const announce = await this.ctx.service.announceService.addLiked(id);
    console.log(announce);
    ctx.body = true;
  }

  /**
   * 找出用户订阅的公告接口
   */
  async getAnnounceByUserSub() {
    const { ctx } = this;
    ctx.body = ctx.request.body;
    let retrunMap = {};
    try {
      const announces = await this.ctx.service.announceService.getAnnounceByUserSub(ctx.body.user_id);

      if (announces) {
        retrunMap = { code: '1', msg: '查询成功', data: announces };
      } else {
        retrunMap = { code: '0', msg: '查询失败' };
      }
    } catch (err) {
      retrunMap = { code: '0', msg: '查询失败,发生异常' };
      console.log(err);
    }
    ctx.body = retrunMap;

  }

}

module.exports = AnnounceController;
